use sea_orm_migration::{prelude::*, schema::*};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(SpiderTask::Table)
                    .if_not_exists()
                    .col(pk_auto(SpiderTask::Id))
                    .col(string(SpiderTask::Name).string_len(128))
                    .col(string(SpiderTask::TargetUrl).string_len(512))
                    .col(string(SpiderTask::UrlPattern).string_len(512))
                    .col(string(SpiderTask::ArticleSelector).string_len(256))
                    .col(string(SpiderTask::BreadcrumbSelector).string_len(256))
                    .col(string(SpiderTask::Status).string_len(32))
                    .col(big_integer(SpiderTask::LastRunAt).null())
                    .col(big_integer(SpiderTask::CrawledCount).default(0))
                    .col(big_integer(SpiderTask::CreatedAt).not_null())
                    .col(big_integer(SpiderTask::UpdatedAt).not_null())
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SpiderTask::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum SpiderTask {
    Table,
    Id,
    Name,
    TargetUrl,
    UrlPattern,
    ArticleSelector,
    BreadcrumbSelector,
    Status,
    LastRunAt,
    CrawledCount,
    CreatedAt,
    UpdatedAt
}
